package com.reefe.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.reefe.dto.DtoUtil;
import com.reefe.dto.ItemDto;
import com.reefe.exception.AppException;
import com.reefe.mapper.TbItemCatMapper;
import com.reefe.mapper.TbItemDescMapper;
import com.reefe.mapper.TbItemMapper;
import com.reefe.pojo.TbItem;
import com.reefe.pojo.TbItemCat;
import com.reefe.pojo.TbItemDesc;
import com.reefe.pojo.TbItemExample;
import com.reefe.service.ItemService;
import com.reefe.utils.IDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiao on 2017-12-11.
 */
@Component
//注意这里使用Dubbo注解方式
@Service(version = "1.0.0" ,timeout = 30000)
//@Transactional
public class ItemServiceImpl implements ItemService {

    @Autowired
    private TbItemMapper itemMapper;
    @Autowired
    private TbItemCatMapper itemCatMapper;
    @Autowired
    private TbItemDescMapper itemDescMapper;

    @Override
    public ItemDto getItemById(long id) {
        ItemDto itemDto = new ItemDto();

        TbItem tbItem = itemMapper.selectByPrimaryKey(id);
        itemDto=DtoUtil.TbItem2ItemDto(tbItem);

        TbItemCat tbItemCat = itemCatMapper.selectByPrimaryKey(itemDto.getCid());
        itemDto.setCname(tbItemCat.getName());

        TbItemDesc tbItemDesc = itemDescMapper.selectByPrimaryKey(id);
        itemDto.setDetail(tbItemDesc.getItemDesc());

        return itemDto;
    }
    @Override
    public TbItem addItem(ItemDto itemDto) {
        long id = IDUtil.getRandomId();
        TbItem tbItem = DtoUtil.ItemDto2TbItem(itemDto);
        tbItem.setId(id);
        tbItem.setStatus((byte) 1);
        tbItem.setCreated(new Date());
        tbItem.setUpdated(new Date());
        if (tbItem.getImage().isEmpty()) {
            tbItem.setImage("http://ow2h3ee9w.bkt.clouddn.com/nopic.jpg");
        }
        if (itemMapper.insert(tbItem) != 1) {
            throw new AppException("添加商品失败");
        }
        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemId(id);
        tbItemDesc.setItemDesc(itemDto.getDetail());
        tbItemDesc.setCreated(new Date());
        tbItemDesc.setUpdated(new Date());

        if (itemDescMapper.insert(tbItemDesc) != 1) {
            throw new AppException("添加商品详情失败");
        }
        //发送消息同步索引库
        //TODO 待开发  发送消息同步索引库
//        sendRefreshESMessage("add",id);
        return getNormalItemById(id);
    }

    @Override
    public Map<String, Object> getItemList(int draw, int start, int length, int cid, String search, String orderCol, String orderDir) {
        PageHelper.startPage(start / length + 1, length); //开始页, 每页条数
        List<TbItem> list = itemMapper.selectItemByCondition(cid, "%" + search + "%", orderCol, orderDir);
        PageInfo<TbItem> pageInfo = new PageInfo<>(list);
        Map<String, Object> info = new HashMap();
        info.put("data", list);
        info.put("recordsTotal", getAllItemCount());
        info.put("recordsFiltered", (int) pageInfo.getTotal());
        info.put("draw", draw);
        return info;
    }

    @Override
    public int getAllItemCount() {
        TbItemExample example = new TbItemExample();
        Long count = itemMapper.countByExample(example);
        return Math.toIntExact(count);
    }

    @Override
    public Map<String, Object> getItemSearchList(int draw, int start, int length, int cid, String search, String minDate, String maxDate, String orderCol, String orderDir) {
        PageHelper.startPage(start / length + 1, length); //开始页, 每页条数
        List<TbItem> list = itemMapper.selectItemByMultiCondition(cid, "%" + search + "%", minDate, maxDate, orderCol, orderDir);
        PageInfo<TbItem> pageInfo = new PageInfo<>(list);
        Map<String, Object> info = new HashMap();
        info.put("data", list);
        info.put("recordsTotal", getAllItemCount());
        info.put("recordsFiltered", (int) pageInfo.getTotal());
        info.put("draw", draw);
        return info;
    }

    @Override
    public TbItem updateItem(Long id,ItemDto itemDto) {

        TbItem oldTbItem=getNormalItemById(id);

        TbItem tbItem= DtoUtil.ItemDto2TbItem(itemDto);

        if(tbItem.getImage().isEmpty()){
            tbItem.setImage(oldTbItem.getImage());
        }
        tbItem.setId(id);
        tbItem.setStatus(oldTbItem.getStatus());
        tbItem.setCreated(oldTbItem.getCreated());
        tbItem.setUpdated(new Date());
        if(itemMapper.updateByPrimaryKey(tbItem)!=1){
            throw new AppException("更新商品失败");
        }

        TbItemDesc tbItemDesc=new TbItemDesc();

        tbItemDesc.setItemId(id);
        tbItemDesc.setItemDesc(itemDto.getDetail());
        tbItemDesc.setUpdated(new Date());
        tbItemDesc.setCreated(oldTbItem.getCreated());

        if(itemDescMapper.updateByPrimaryKey(tbItemDesc)!=1){
            throw new AppException("更新商品详情失败");
        }
        //TODO 待开发  发送消息同步索引库
        //同步缓存
//        deleteProductDetRedis(id);
        //发送消息同步索引库
//        sendRefreshESMessage("add",id);
        return getNormalItemById(id);
    }

    @Override
    public int deleteItem(long id) {
        //删除商品
        if(itemMapper.deleteByPrimaryKey(id)!=1){
            throw new AppException("删除商品失败");
        }
        //删除描述
        if(itemDescMapper.deleteByPrimaryKey(id)!=1){
            throw new AppException("删除商品详情失败");
        }
        //发送消息同步索引库
//        sendRefreshESMessage("delete",id);
        return 0;
    }

    @Override
    public TbItem alertItemState(Long id, Integer state) {
        TbItem tbMember = getNormalItemById(id);
        tbMember.setStatus(state.byteValue());
        tbMember.setUpdated(new Date());

        if (itemMapper.updateByPrimaryKey(tbMember) != 1) {
            throw new AppException("修改商品状态失败");
        }
        return getNormalItemById(id);
    }

    @Override
    public TbItem getNormalItemById(Long id) {

        return itemMapper.selectByPrimaryKey(id);
    }
}
